﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CloudClip.Properties;
using ExtensionLibrary.Exception;

namespace CloudClip
{
    public partial class frmSetting : Form
    {
        public Setting _Setting;
        public frmMain _frmMain;

        public frmSetting(frmMain fm)
        {
            InitializeComponent();
            InitHokeyCombox();

            if (SettingHelper.CheckSetting())
            {
                _Setting = SettingHelper.Load();
            }else
            {
                _Setting = new Setting();
                _Setting.ClipSize = 20;
            }
            
            this.Text = frmMain.AppName;
            this.Icon = Resources.Icon;

            _frmMain = fm;

            this.txtPath.Text = _Setting.ClipFolderPath;
            this.numericUpDown1.Value = _Setting.ClipSize;
            checkFolderPath();
            cbIsAutoPast.Checked = _Setting.IsAutoPast;

            if (_Setting != null)
            {
                this.comboBox1.SelectedItem = (Keys)_Setting.HotKey1;
                this.comboBox2.SelectedItem = (Keys)_Setting.HotKey2;
                this.comboBox3.SelectedItem = (Keys)_Setting.HotKey3;
            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            if (_Setting == null)
                _Setting = new Setting();

            _Setting.ClipFolderPath = this.txtPath.Text;
            _Setting.ClipSize = this.numericUpDown1.Value;

            HotKeyComboxItem item1 = this.comboBox1.SelectedItem as HotKeyComboxItem;
            HotKeyComboxItem item2 = this.comboBox2.SelectedItem as HotKeyComboxItem;
            HotKeyComboxItem item3 = this.comboBox3.SelectedItem as HotKeyComboxItem;

            _Setting.HotKey1 = Convert.ToInt32(item1.HotKey);
            _Setting.HotKey2 = Convert.ToInt32(item2.HotKey);
            _Setting.HotKey3 = Convert.ToInt32(item3.HotKey);

            _Setting.IsAutoPast = cbIsAutoPast.Checked;

            _Setting.Save();
            _frmMain.Init();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();

            if (path.SelectedPath != string.Empty)
                this.txtPath.Text = path.SelectedPath;

            checkFolderPath();
        }

        private void checkFolderPath()
        {
            if (this.txtPath.Text == string.Empty)
            {
                this.txtPath.Text = "請先選擇一個資料夾";
            }
        }

        public void InitHokeyCombox()
        {
            this.comboBox1.Items.Add(new HotKeyComboxItem("ALT", Keys.Alt));
            this.comboBox1.Items.Add(new HotKeyComboxItem("CONTROL", Keys.Control));
            this.comboBox1.Items.Add(new HotKeyComboxItem("SHIFT", Keys.Shift));

            this.comboBox2.Items.Add(new HotKeyComboxItem("ALT", Keys.Alt));
            this.comboBox2.Items.Add(new HotKeyComboxItem("CONTROL", Keys.Control));
            this.comboBox2.Items.Add(new HotKeyComboxItem("SHIFT", Keys.Shift));

            this.comboBox3.Items.Add(new HotKeyComboxItem("A", Keys.A));
            this.comboBox3.Items.Add(new HotKeyComboxItem("B", Keys.B));
            this.comboBox3.Items.Add(new HotKeyComboxItem("C", Keys.C));
            this.comboBox3.Items.Add(new HotKeyComboxItem("D", Keys.D));
            this.comboBox3.Items.Add(new HotKeyComboxItem("E", Keys.E));
            this.comboBox3.Items.Add(new HotKeyComboxItem("F", Keys.F));
            this.comboBox3.Items.Add(new HotKeyComboxItem("G", Keys.G));
            this.comboBox3.Items.Add(new HotKeyComboxItem("H", Keys.H));
            this.comboBox3.Items.Add(new HotKeyComboxItem("I", Keys.I));
            this.comboBox3.Items.Add(new HotKeyComboxItem("J", Keys.J));
            this.comboBox3.Items.Add(new HotKeyComboxItem("K", Keys.K));
            this.comboBox3.Items.Add(new HotKeyComboxItem("L", Keys.L));
            this.comboBox3.Items.Add(new HotKeyComboxItem("M", Keys.M));
            this.comboBox3.Items.Add(new HotKeyComboxItem("N", Keys.N));
            this.comboBox3.Items.Add(new HotKeyComboxItem("O", Keys.O));
            this.comboBox3.Items.Add(new HotKeyComboxItem("P", Keys.P));
            this.comboBox3.Items.Add(new HotKeyComboxItem("Q", Keys.Q));
            this.comboBox3.Items.Add(new HotKeyComboxItem("R", Keys.R));
            this.comboBox3.Items.Add(new HotKeyComboxItem("S", Keys.S));
            this.comboBox3.Items.Add(new HotKeyComboxItem("T", Keys.T));
            this.comboBox3.Items.Add(new HotKeyComboxItem("U", Keys.U));
            this.comboBox3.Items.Add(new HotKeyComboxItem("V", Keys.V));
            this.comboBox3.Items.Add(new HotKeyComboxItem("W", Keys.W));
            this.comboBox3.Items.Add(new HotKeyComboxItem("X", Keys.X));
            this.comboBox3.Items.Add(new HotKeyComboxItem("Y", Keys.Y));
            this.comboBox3.Items.Add(new HotKeyComboxItem("Z", Keys.Z));
        }

        private void frmSetting_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void frmSetting_Load(object sender, EventArgs e)
        {

        }

        private void frmSetting_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }

    public class HotKeyComboxItem
    {
        public HotKeyComboxItem(string sDisplay, Keys key)
        {
            DisplayText = sDisplay;
            HotKey = key;
        }

        public string DisplayText;

        public Keys HotKey;

        public override string ToString()
        {
            return DisplayText;
        }

        public override bool Equals(object obj)
        {
            if (obj == null || obj is DBNull)
                return false;

            if (Convert.ToInt32(HotKey) == Convert.ToInt32(obj))
            {
                return true;
            }

            return false;
        }
    }
}
